package com.stn.zjx.givingtreasure.activity.bi;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.BackupMnEvent;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.service.WalletUserService;
import com.stn.zjx.givingtreasure.table.WalletUser;
import com.vondear.rxtools.RxActivityTool;


import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FirstCreateEthBackUpActivity extends BaseActivity {

    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.more_bt)
    RelativeLayout moreBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.backup_bt)
    ButtonBgUi backupBt;

   public  String address="";
    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        actionTitle.setText(getString(R.string.backupwallet));
         address=getIntent().getStringExtra("address");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_create_eth_backup);
        ButterKnife.bind(this);
        initComponent();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBackupMnemonic(BackupMnEvent backupMnEvent){
        finish();
    }



    public void initInputPassword(){
        Dialog dialog=new Dialog(this,R.style.dialog_soft_input);
         dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
         dialog.setContentView(R.layout.dialog_input_wallet_password);
       EditText passwordEdit=dialog.findViewById(R.id.password_edit);
              View cancel_bt=dialog.findViewById(R.id.cancel_bt);
        cancel_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                   dialog.dismiss();
            }
        });
        View sure_bt=dialog.findViewById(R.id.sure_bt);
        sure_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               WalletUser db=WalletUserService.getInstance(getApplicationContext())
                        .getWalletUser(address);
               if(db==null){
                    showToast(getString(R.string.db_error));
                   return;
               }
                if(db.getTradepassword().equals(passwordEdit.getText().toString().trim())){
                   Bundle bundle=new Bundle();
                 bundle.putString("mnemonic",db.getMnemonic());
                    RxActivityTool.skipActivity(FirstCreateEthBackUpActivity.this,BackUpMnemonicActivity.class,bundle);
                   dialog.dismiss();
                }else{
                   showToast(getString(R.string.wallet_password_error));
                }

            }
        });
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {

             //   SoftInputUtils.openSoftInput(FirstCreateEthBackUpActivity.this, dialog.getCurrentFocus(), 50);

            }
        });
         dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
             @Override
             public void onDismiss(DialogInterface dialogInterface) {
          //       SoftInputUtils.closedSoftInput(FirstCreateEthBackUpActivity.this);
//                im.hideSoftInputFromWindow(guest_content_edit.getWindowToken(),0);
                 passwordEdit.getText().clear();
             }
         });
        dialog.show();

    }

    @OnClick({R.id.back_bt,R.id.backup_bt})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.backup_bt:
               initInputPassword();
                break;
        }
    }


}
